package xyz.yuzegod.megawalls.classes.mythic.phoenix;

import org.bukkit.Location;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.util.Vector;
import xyz.yuzegod.megawalls.MegaWalls;
import xyz.yuzegod.megawalls.classes.Classes;
import xyz.yuzegod.megawalls.classes.Skill;
import xyz.yuzegod.megawalls.game.GamePlayer;
import xyz.yuzegod.megawalls.stats.KitStatsContainer;
import xyz.yuzegod.megawalls.util.ParticleEffect;
import xyz.yuzegod.megawalls.util.PlayerUtils;
import xyz.yuzegod.megawalls.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class ThirdSkill extends Skill
{
    public static List<GamePlayer> gamePlayers;
    
    public ThirdSkill(final Classes classes) {
        super("凤凰涅槃", classes);
    }
    
    @Override
    public int maxedLevel() {
        return 3;
    }
    
    @Override
    public double getAttribute(final int level) {
        switch (level) {
            case 1: {
                return 0.6;
            }
            case 2: {
                return 0.8;
            }
            case 3: {
                return 1.0;
            }
            default: {
                return 0.6;
            }
        }
    }
    
    @Override
    public List<String> getInfo(final int level) {
        final List<String> lore = new ArrayList<String>();
        if (level == 1) {
            lore.add(" §8▪ §7受到致命伤害时,将会暂时石化并同时恢复§a" + StringUtils.percent(this.getAttribute(level)) + "§7的生命值");
            lore.add(" §8▪ §7在涅槃结束时你获得虚弱IV、速度II和抗性提升II,持续10秒");
            lore.add(" §8▪ §7每局游戏你只能涅槃一次");
            lore.add(" §8▪ §7该效果在凋零死亡后生效");
            lore.add("   §7凤凰涅槃对你的契约队友也生效一次,恢复其§a" + StringUtils.percent(this.getAttribute(level) / 2.0) + "§7的生命值");
            return lore;
        }
        lore.add(" §8▪ §7受到致命伤害时,将会暂时石化并同时恢复§8" + StringUtils.percent(this.getAttribute(level - 1)) + " ➜ §a" + StringUtils.percent(this.getAttribute(level)) + "§7的生命值");
        lore.add("   §7凤凰涅槃对你的契约队友也生效一次,恢复其§8" + StringUtils.percent(this.getAttribute(level - 1) / 2.0) + " ➜ §a" + StringUtils.percent(this.getAttribute(level) / 2.0) + "§7的生命值");
        return lore;
    }
    
    @Override
    public void upgrade(final GamePlayer gamePlayer) {
        final KitStatsContainer kitStats = gamePlayer.getPlayerStats().getKitStats(this.getClasses());
        kitStats.addSkill3Level();
    }
    
    @Override
    public int getPlayerLevel(final GamePlayer gamePlayer) {
        return gamePlayer.getPlayerStats().getKitStats(this.getClasses()).getSkill3Level();
    }
    
    @Override
    public boolean use(final GamePlayer gamePlayer, final KitStatsContainer kitStats) {
        use(gamePlayer);
        return true;
    }
    
    public static void use(final GamePlayer gamePlayer) {
        if (ThirdSkill.gamePlayers.contains(gamePlayer)) {
            return;
        }
        Phoenix.skill3.add(gamePlayer);
        final Player player = gamePlayer.getPlayer();
        PlayerUtils.heal(player, 40.0 * (gamePlayer.getGameTeam().isWitherDead() ? 0.3 : 0.6));
        ThirdSkill.gamePlayers.add(gamePlayer);
        final ArmorStand as = (ArmorStand)player.getWorld().spawnEntity(player.getLocation(), EntityType.ARMOR_STAND);
        as.setVisible(false);
        as.setPassenger((Entity)player);
        ParticleEffect.FLAME.display(2.0f, 2.0f, 2.0f, 0.0f, 5, player.getLocation().clone().add(0.0, 1.0, 0.0), 10.0);
        as.setVelocity(new Vector(0.0, 2.0, 0.0));
        new BukkitRunnable() {
            int ticks = 0;
            
            public void run() {
                if (this.ticks >= 60) {
                    as.setPassenger((Entity)null);
                    as.remove();
                    Phoenix.skill3.remove(gamePlayer);
                    player.addPotionEffect(new PotionEffect(PotionEffectType.WEAKNESS, 200, 3));
                    player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 200, 1));
                    player.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 200, 1));
                    this.cancel();
                    return;
                }
                final Location location = as.getLocation().clone();
                location.add(0.0, 0.5, 0.0);
                as.teleport(location);
                ++this.ticks;
            }
        }.runTaskTimer((Plugin)MegaWalls.getInstance(), 0L, 1L);
    }
    
    static {
        ThirdSkill.gamePlayers = new ArrayList<GamePlayer>();
    }
}
